/*
 * UIHexagon.fx
 *
 * Created on Oct 2, 2009, 12:16:08 PM
 */

package dawnland02.ui;

import java.util.List;

import javafx.geometry.BoundingBox;
import javafx.scene.*;
import javafx.scene.layout.Container;
import javafx.scene.layout.LayoutInfo;
import javafx.scene.layout.Resizable;
import javafx.scene.layout.Stack;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

import dawnland02.data.map.model.MatrixCell;
import dawnland02.model.Cell;
import dawnland02.model.quest.QuestFactory;
import dawnland02.model.quest.QuestManager;
import javafx.geometry.HPos;
import javafx.geometry.VPos;


/**
 * @author Petru Obreja (obrejap@yahoo.com)
 */

public class UIHexagon extends CustomNode, Resizable, Cell{
  package var matrixCell : MatrixCell;
  package var uiService : UIService;  

  def prefWidth = uiService.getUIHexagonPrefWidth();
  def prefHeight = uiService.getUIHexagonPrefHeight();

  override var x = matrixCell.getX();
  override var y = matrixCell.getY();
  override var evenRow = matrixCell.isEvenRow();

  package var centerXToParent : Number;
  package var centerYToParent : Number;
  var uiRoad : UIRoad;

  init {
      def evenRowShift = if (evenRow) prefWidth/2 else 0.0;
      centerXToParent = x * prefWidth + evenRowShift + prefWidth / 2;
      centerYToParent = y * prefHeight * 3/4 + prefHeight / 2;          
  }


  override function getPrefWidth(height) {prefWidth}
  override function getPrefHeight(width) {prefHeight}

  override var layoutBounds = bind lazy BoundingBox { //bind lazy: if will be updated only when called
        //T0910021.2: remove bind lazy for UIHexagon.layoutBounds
      minX: 0
      minY: 0
      width: width
      height: height
  }

  override function create(): Node {
          def xStep = uiService.getXStep();
          def yStep = uiService.getYStep();
          def polygon = Polygon {
              points: [
                  xStep, 0.0, //up
                  2*xStep, yStep, //right-up
                  2*xStep, 3*yStep, //right-down
                  xStep, 4*yStep, //down
                  0.0, 3*yStep, //left-down
                  0.0, yStep //left-up
                  ]
              fill: uiService.getColor(matrixCell.getColor())
              stroke: Color.DARKSEAGREEN
              strokeWidth: 1.0
              onMousePressed: function(event) {
                  var quest;
                  if(event.secondaryButtonDown){
                      if(event.controlDown){
                          quest = QuestFactory.createBuildRoadToQuest(matrixCell.getId());
                          QuestManager.getQuestManager().addQuest(quest); //T0910092: make calls to QuestManager via GeneralService
                      } else {
                          quest = QuestFactory.createMoveToQuest(matrixCell.getId());
                          QuestManager.getQuestManager().addQuest(quest); //T0910092: make calls to QuestManager via GeneralService
                        }
                      }
                  }
          }

          /*def circle = Circle {
              centerX: xStep;
              centerY: 2 * yStep;
              radius: yStep;
              stroke: Color.RED;
              strokeWidth: bind (roadLevel + 1.0);
              fill: Color.TRANSPARENT
          }*/
          uiRoad = UIRoad {
                matrixCell: matrixCell;
                centerX: xStep;
                centerY: 2 * yStep;
                radius: yStep;
                roadLevel: matrixCell.getRoadLevel();
                uiService: uiService;
          }


          def text = Text {
              content: matrixCell.getId().toString();
              fill: Color.DARKSEAGREEN;
              font: Font.font("Courier", FontWeight.BOLD, yStep)
          }

          Container{
              content:[
                  Stack{
                      content:[polygon, text]
                      },
                  uiRoad]
          }

                 /*Stack{
                      content:[polygon, uiRoad]
                      }*/

  }

  public function getId() : Integer {
      return matrixCell.getId();
  }

  public function getCenterXToParent() : Number {
      return centerXToParent;
  }

  public function getCenterYToParent() : Number {
      return centerYToParent;
  }

  public function updateRoadLevel(roadLevel : Number){
      uiRoad.roadLevel = roadLevel;
  }

}
